package gautobgchanger;

import java.io.File;
import java.util.Scanner;

/**
 * This class is for reading the config file of the gautobg-changer
 * without it the folders and interval would have been quite static
 *
 * @author b1o
 */
public class ConfReader {

    //A configuration file
    private File conf;
    //The path of the Configuration file
    private File confPath;
    //A scanner object for scanning text files
    private Scanner sc;
    //The output of either the path or the interval
    private String output;
    //The jarfile of this program
    private File jarFile;

    /**
     * Gets the path and, creates the file object for the configuration file
     */
    public ConfReader() {

        try {
            //Gets the JarFile path and stores it in a file object, so that you can start the application from anywhere
            //and still the program would know where the config file is.
            jarFile = new File(Main.class.getProtectionDomain().getCodeSource().getLocation().toURI());
        } catch (Exception e) {
        }
        //splits the string where there is a slash and stores each part in an array
        String[] temp = jarFile.getAbsolutePath().split("/");
        String path = "";
        //loops trough all of the String parts except for the last one containing the jar file name.
        //we do this to get the parent folder of the jar file
        for (int i = 0; i < temp.length - 1; i++) {
            //puts them back together again
            path = path + temp[i] + "/";
        }
        //path to the config file
        confPath = new File(path + "/conf/");
        //path of the config file
        conf = new File(path + "/conf/changer.conf");
        output = "";
    }

    /**
     * This methode gets the path where the wallpapers are stored
     * It scans the file for the string "wallpaper_folder", If it
     * finds it, it changes the cursor of the scanner to two points
     * to the right, so it's looing for a file that has these values:
     * "wallpaper_folder = /some/path/with/wallpapers"
     * @return The path of the wallpapers
     * @throws Exception
     */
    public String getPath() throws Exception {
        sc = new Scanner(conf);
        while (sc.hasNext()) {
            output = sc.next().toLowerCase();
            if (output.equals("wallpaper_folder")) {
                sc.next();
                output = sc.next();
                break;
            }
        }
        sc.close();
        return output;
    }

    /**
     * This methode gets the interval for each wallpaper change
     * It scans the file for the string "interval", If it
     * finds it, it changes the cursor of the scanner to two points
     * to the right, so it's looing for a string that has these values:
     * "interval = <code>some_number<code>"
     * @return The interval set by the user, or in the config file
     * @throws Exception
     */
    public int getInterval() throws Exception {
        sc = new Scanner(conf);
        while (sc.hasNext()) {
            output = sc.next().toLowerCase();
            if (output.equals("interval")) {
                sc.next();
                output = sc.next();
                break;
            }
        }
        sc.close();
        return Integer.parseInt(output);
    }

    /**
     * @return The configuration file
     */
    public File getFile() {
        return conf;
    }

    /**
     * Checks if a config file excists
     * @return If a file excists, return true. else return false
     */
    public boolean configFileExcists() {
        if (conf.exists()) {
            return true;
        }
        return false;
    }
    /**
     * Gets the path of the config File
     * @return The configuration path
     */
    public File getConfPath() {
        return confPath;
    }
}
